package com.lsk.admin.shop;

import com.jfinal.kit.HttpKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.lsk.util.JsonUtil;
import com.lsk.util.UuidUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 闲卡网
 */
public class testAPIService {


    public static void cardSubmit() {

        String product_detail_id = "";//商品id

        String product_type_id = "";//商品类型id

        String product_id = "";//商品id

        String cards = "";//卡片账号密钥

        String third_order_sn = "";//第三方单号

    }
    public static void main(String ats[]){
        new testAPIService().getCards();
    }


    /**
     * 卡列表 查找接口中所有卡类型的卡的所有面值信息存入数据库
     */
    public List<Record> getCards() {
        String dev_id = "100006338";//接口id

        String dev_token = "e616d903fac9df367752b4edc43a36df";

        String cardSubmitUrl = "http://api.xiankawang.com/league/procreate";

        String cardListUrl = "http://api.xiankawang.com/league/product";

        Map<String, String> parameters = new HashMap<>();

        parameters.put("dev_id", dev_id);

        parameters.put("dev_token", dev_token);

        String result = HttpKit.post(cardListUrl, parameters, "");

        Map<String, Object> resultMap = JsonUtil.getMapFromJsonObjStr(result);
        List<JSONObject> dataList = (List<JSONObject>) resultMap.get("data");//接口中的data数组
        List<Record> myList = new ArrayList<>();        //结果
        for (JSONObject r : dataList) {
            if (r == null) {
                continue;
            }
            String commodityTypeName = r.get("name").toString();//卡类型名称
            JSONArray array = r.getJSONArray("products");       //所有的卡
            for (int i = 0; i < array.size(); i++) {
                if (array.getJSONObject(i) == null) {
                    continue;
                }
                String commodityName = array.getJSONObject(i).get("name").toString();   //卡名称
                JSONArray details = array.getJSONObject(i).getJSONArray("product_details");//所有的卡面值
                for (int j = 0; j < details.size(); j++) {
                    JSONObject detail = details.getJSONObject(j);
                    String commodityId = detail.get("product_id").toString();       //卡id
                    String commodityType = detail.get("product_type_id").toString();    //卡类型id
                    String recycleDiscount = detail.get("recycle_discount").toString(); //卡的折扣
                    String fieldType = detail.get("field_type").toString();
                    String commodityDetailId = detail.get("id").toString();
                    String commodityDetailFaceValue=detail.get("face_value").toString();
                    Record re = new Record().set("commodityId", commodityId).set("id", UuidUtil.get32UUID())
                            .set("commodityTypeId", commodityType).set("commodityName", commodityName)
                            .set("commodityTypeName", commodityTypeName).set("recycleDiscount", recycleDiscount)
                            .set("fieldType", fieldType).set("commodityDetailId", commodityDetailId).set("commodityFaceValue",commodityDetailFaceValue);
                    myList.add(re);
                }

            }

        }
        for (Record record : myList) {
            /**
             * 判断数据库中是否存在 要存的面值信息
             */

            int count = 0;
            count = Db.queryInt("select count(*) from xy_commodity where commodityDetailId='" + record.getStr("commodityDetailId") + "'"
                    + "and  commodityId ='" + record.get("commodityId") + "'"
            );
            if (count == 0) {
                Db.save("xy_commodity", record);
            } else if (count == 1) {        //  判断数据库中的数据折扣是否更新
                String newRecycleDiscount = record.getStr("recycleDiscount");
                String newCommodityDetailFaceValue=record.getStr("commodityFaceValue");
                Record temp =Db.findFirst("select * from xy_commodity where commodityDetailId='" + record.getStr("commodityDetailId") + "'"
                        + " and  commodityId ='" + record.getStr("commodityId") + "'");
                String myRecycleDiscount=temp.getStr("recycleDiscount");
                String myCommodityDetailFaceValue=temp.getStr("commodityFaceValue");
                if (!myRecycleDiscount.equals(newRecycleDiscount)) {
                    Db.update("update xy_commodity set recycleDiscount = ? where commodityDetailId=? and " +
                            "commodityId =?",newRecycleDiscount,record.getStr("commodityDetailId"),record.get("commodityId"));
                }else if(!myCommodityDetailFaceValue.equals(newCommodityDetailFaceValue)){
                    Db.update("update xy_commodity set commodityFaceValue = ? where commodityDetailId=? and " +
                            "commodityId =?",newCommodityDetailFaceValue,record.getStr("commodityDetailId"),record.get("commodityId"));
                }
            }
        }

        return myList;
    }


}
